﻿namespace Appify
{
    using System;
    using System.ComponentModel;
    using System.ComponentModel.Design;
    using System.Windows.Forms;

    public class AppDirectoryActionList : DesignerActionList
    {
        #region Fields

        private readonly AppDirectory control;

        private DesignerActionUIService designerActionUISvc;

        #endregion

        //The constructor associates the control 
        //with the smart tag list.

        #region Constructors and Destructors

        public AppDirectoryActionList(IComponent component)
            : base(component)
        {
            this.control = (AppDirectory)component;

            // Cache a reference to DesignerActionUIService, so the
            // DesigneractionList can be refreshed.
            this.designerActionUISvc = this.GetService(typeof(DesignerActionUIService)) as DesignerActionUIService;
        }

        #endregion

        // Helper method to retrieve control properties. Use of 
        // GetProperties enables undo and menu updates to work properly.

        #region Public Properties

        public AppSource Source
        {
            get
            {
                return this.control.Source;
            }
            set
            {
                this.control.Source = value;
            }
        }

        #endregion

        // Implementation of this abstract method creates smart tag  
        // items, associates their targets, and collects into list.

        #region Public Methods and Operators

        public override DesignerActionItemCollection GetSortedActionItems()
        {
            var items = new DesignerActionItemCollection();

            items.Add(new DesignerActionMethodItem(this, "SetDock", this.getDockText(), "", "", true));
            items.Add(new DesignerActionPropertyItem("Source", "Source"));

            return items;
        }

        public void SetDock()
        {
            this.control.Dock = this.control.Dock == DockStyle.Fill ? DockStyle.None : DockStyle.Fill;
        }

        #endregion

        #region Methods

        private PropertyDescriptor GetPropertyByName(String propName)
        {
            PropertyDescriptor prop;
            prop = TypeDescriptor.GetProperties(this.control)[propName];
            if (null == prop)
            {
                throw new ArgumentException("Matching AppDirectory property not found!", propName);
            }
            return prop;
        }

        private string getDockText()
        {
            return this.control.Dock == DockStyle.Fill ? "Undock to parent container" : "Dock to parent container";
        }

        #endregion
    }
}